<?php
include_once("round_schedule.php");
if(class_exists('SQL_Helper') != true)
{
   include_once("sql_helper.php");
}
class RoundScheduleRepository
{
	function readRoundSchedule($round_scheduleID){
		return SQL_Helper::executeQuerySingle("Select * from `round_schedule` where `round_scheduleID` = ?", $round_scheduleID);	
	}
	
	function deleteRoundSchedule($round_scheduleID){
		SQL_Helper::executeNonQuery("delete from `round_schedule` where `round_scheduleID` = ?", $round_scheduleID);
	}
	
	function createRoundSchedule($roundID, $team1, $team2, $table,$type){
		SQL_Helper::executeNonQuery("INSERT INTO `round_schedule` (`round_scheduleID`, `roundID`, `team1`, `team2`,`Table`,`type`) VALUES(null, ?, ?, ?, ?,?) ",$roundID, $team1, $team2,$table,$type);
	}
	
	function map($row){
		$class= "RoundSchedule";
		return SQL_Helper::sql_map($row,$class);
	}
	
	function maps($rows){
		$class= "RoundSchedule";
		return SQL_Helper::sql_maps($rows,$class);
	}
	
	function getScheduleByRoundID($roundID){
		return SQL_Helper::executeQuery("Select * from `round_schedule` where `roundID` = ?", $roundID);
	}
	
	function getScheduleByRoundIDAndTeamID($roundID, $teamID){
		return SQL_Helper::executeQuerySingle("Select * from `round_schedule` where `roundID` = ? and (`team1`=? or `team2`=?)", $roundID,$teamID,$teamID);
	}
	
	function getSchedulesBeforeRound($firstRoundID, $lastRoundID, $teamID){
		return SQL_Helper::executeQuery("Select * from `round_schedule` where `roundID`>=? and  `roundID`<=? AND (`type` NOT LIKE  'final' OR  `type` IS NULL) and (`team1`=? or `team2`=?)", $firstRoundID, $lastRoundID,$teamID,$teamID);
	}
	
	function getScheduleByRoundIDAndDivisionID($roundID, $divisionID){
		return (SQL_Helper::executeQuery("SELECT * FROM `round_schedule` as `x1` inner join `teams` as `x2` on `x1`.`team1`=`x2`.`teamID` WHERE `roundID`=? and `x2`.`divisionID`=?", $roundID,$divisionID));
	}
	
	function deleteTables($roundID){
		SQL_Helper::executeNonQuery("update `round_schedule` set `Table`=null where `roundID` = ?", $roundID);
	}
	
	function addTable($round_scheduleID, $table){
		SQL_Helper::executeNonQuery("update  `round_schedule` set `Table`=? where `round_scheduleID` = ?  ",$table, $round_scheduleID);
	}
	
}
?>